package com.zhang.matcher;

import com.zhang.mailet.HelloMailet;
import org.apache.james.core.MailAddress;
import org.apache.mailet.Mail;
import org.apache.mailet.base.GenericMatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.mail.MessagingException;
import java.util.Collection;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class HelloMatcher extends GenericMatcher {

    private static final Logger LOGGER = LoggerFactory.getLogger(HelloMatcher.class);

    @Override
    public Collection<MailAddress> match(Mail mail) throws MessagingException {
        Collection<MailAddress> collectio= mail.getRecipients().stream().filter(a -> {
            LOGGER.info(a.getLocalPart());
            LOGGER.info("name="+a.getDomain().name());
            LOGGER.info("{}",a.getDomain().name().equals("james"));
            return a.getDomain().name().equals("james");
        }).collect(Collectors.toList());
        LOGGER.info("{}",collectio);
        return collectio;
    }
}
